package com.yeyks.commonReference.append.mongodb.service.impl;

import com.yeyks.commonReference.append.mongodb.entity.CnHotelImageMongoEntity;
import com.yeyks.commonReference.append.mongodb.repository.CnHotelImageMongoRepository;
import com.yeyks.commonReference.append.mongodb.service.CnHotelImageMongoMongoService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CnHotelImageMongoMongoServiceImpl implements CnHotelImageMongoMongoService {

    private final CnHotelImageMongoRepository cnHotelImageMongoRepository;

    @Override
    public void save(CnHotelImageMongoEntity cnHotelImageMongoEntity) {
        try {
            // 首先查询是否存在，存在删除
            Optional<CnHotelImageMongoEntity> byId = cnHotelImageMongoRepository.findById(cnHotelImageMongoEntity.getSourceId());
            if (byId.isPresent()) {
                cnHotelImageMongoRepository.deleteById(cnHotelImageMongoEntity.getSourceId());
            }
            cnHotelImageMongoRepository.save(cnHotelImageMongoEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public CnHotelImageMongoEntity get(String sourceId) {

        try {
            // 首先查询是否存在，存在删除
            Optional<CnHotelImageMongoEntity> byId = cnHotelImageMongoRepository.findById(sourceId);
            if (byId.isPresent()) {
                return byId.get();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
